from pygame import display as pdis
from pygame.rect import Rect


class ZNode ( object ) :
	def __init__ ( self , father = None , rect = pdis.get_surface ( ).get_rect ( ) ) :
		self.screen = pdis.get_surface ( )
		self._running = True
		self.children = [ ]
		self.father = father
		self.rect = Rect ( rect )

	def _add ( self , *args ) :
		if len ( args ) :
			for x in args :
				self.children.append ( x )
		else :
			return False

	def _remove ( self , *args ) :
		for x in args :
			for y in self.children :
				if x == y.__class__.__name__ :
					self.children.remove ( y )

	def _exist ( self , args ) :
		for y in self.children :
			if args == y.__class__.__name__ :
				print args + ' ' + str ( type ( y ) ) + ' is exist'
				return True

	def draw ( self ) :
		if self._running :
			if len ( self.children ) :
				for x in self.children :
					x.draw ( )

	def update ( self , event , pos , key ) :
		if self._running :
			if len ( self.children ) :
				for child in self.children :
					child.update ( event , pos , key )

	def _clear ( self ) :
		del self.children [ : ]

	def start ( self ) :
		self._running = True

	def stop ( self ) :
		self._running = False

	@property
	def _size ( self ) :
		return self.rect.size

	@_size.getter
	def _size ( self ) :
		return self.rect.size

	@_size.setter
	def _size ( self , size ) :
		self.rect.size = size

	@property
	def _pos ( self ) :
		return self.rect.topleft

	@_pos.getter
	def _pos ( self ) :
		return self.rect.topleft

	@_pos.setter
	def _pos ( self , pos ) :
		self.rect.topleft = map ( lambda (a , b ) : a + b , zip ( self.father._pos , pos ) )
